<?php

namespace magein\laravel\seecms\command;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class Seed extends Command
{
    /**
     * 命令名称及签名
     *
     * @var string
     */
    protected $signature = 'seecms:seed';

    /**
     * 命令描述
     *
     * @var string
     */
    protected $description = 'seecms seed';

    protected $help = '
    php artisan seecms:seed   初始化mysql表数据
';

    public function handle()
    {
        $this->menu();
        $this->rule();
        $this->role();
        $this->user();
    }

    protected function menu()
    {
        $table_name = 'seecms_menu';

        $concat = function ($parent_id, $title, $icon, $url, $sort, $open_type) {
            return [
                'parent_id' => $parent_id,
                'title' => $title,
                'icon' => $icon,
                'url' => $url,
                'sort' => $sort,
                'type' => 1,
                'open_type' => $open_type,
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s'),
            ];
        };

        $data = DB::table($table_name)->where('id', 1)->first();
        if (empty($data)) {
            DB::table($table_name)->insert(
                [
                    $concat('0', '用户管理', 'iconfont icon-user-settings-line', '', '20', '_iframe'),
                    $concat('1', '用户管理', 'iconfont icon-user-settings-line', 'p/user/page', '10', '_iframe'),
                    $concat('1', '角色管理', 'iconfont icon-user-shared-2-line', 'p/userRole/page', '20', '_iframe'),
                    $concat('1', '菜单管理', 'iconfont icon-other', 's/permission/menu', '30', '_iframe'),
                    $concat('1', '权限规则', 'iconfont icon-user-settings-line', 'p/rule/page', '40', '_iframe'),
                    $concat('1', '权限申请', 'iconfont icon-unlock', 'p/userRuleApply/page', '50', '_iframe'),
                    $concat('1', '登录日志', 'iconfont icon-history', 'p/userLogin/page', '999', '_iframe'),
                    $concat('0', '常规管理', 'iconfont icon-setup', '', '10', '_iframe'),
                    $concat('8', '系统配置', 'iconfont icon-manage', 's/general/setting', '100', '_iframe'),
                    $concat('8', '附件管理', 'iconfont icon-picture', 'p/attachment/page', '200', '_iframe'),
                    $concat('8', '数据库备份', 'iconfont icon-database-2-line', 's/database/setting', '300', '_iframe'),
                    $concat('8', '帮助中心', 'iconfont icon-oschina', 'https://gitee.com/magein/php-seecms', '973', '_blank'),
                    $concat('8', '微信设置', 'iconfont icon-wechat-2-fill', 's/wechat/setting', '400', '_iframe'),
                ]
            );
        }
    }

    protected function rule()
    {
        $table_name = 'seecms_rule';

        $concat = function ($group, $title, $path) {
            return [
                'group' => $group,
                'title' => $title,
                'url' => $path,
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s'),
            ];
        };

        $data = Db::table($table_name)->where('id', 1)->first();

        if (empty($data)) {
            Db::table($table_name)->insert(
                [
                    $concat('attachment', '附件列表', 'p/attachment/index'),
                    $concat('attachment', '上传', 'p/attachment/save'),
                    $concat('attachment', '附件查看', 'p/attachment/re'),
                    $concat('attachment', '附件删除', 'p/attachment/del'),
                    $concat('user', '用户列表', 'p/user/index'),
                    $concat('user', '用户创建', 'p/user/create'),
                    $concat('user', '保存用户', 'p/user/save'),
                    $concat('user', '用户授权页面', 's/permission/userGrant'),
                    $concat('user', '用户查看', 'p/user/re'),
                    $concat('user', '用户编辑页面', 'p/user/edit'),
                    $concat('user', '更新用户', 'p/user/update'),
                    $concat('user', '用户删除', 'p/user/del'),
                    $concat('user_role', '用户角色列表', 'p/userRole/index'),
                    $concat('user_role', '用户角色创建', 'p/userRole/create'),
                    $concat('user_role', '保存用户角色', 'p/userRole/save'),
                    $concat('user_role', '角色授权页面', 's/permission/roleGrant'),
                    $concat('user_role', '用户角色edit', 'p/userRole/edit'),
                    $concat('user_role', '更新用户角色', 'p/userRole/update'),
                    $concat('user_role', '用户角色删除', 'p/userRole/del'),
                    $concat('menu', '菜单列表', 'p/menu/index'),
                    $concat('menu', '菜单新增页面', 'p/menu/create'),
                    $concat('menu', '保存菜单', 'p/menu/save'),
                    $concat('menu', '菜单查看', 'p/menu/re'),
                    $concat('menu', '菜单编辑页面', 'p/menu/edit'),
                    $concat('menu', '更新菜单', 'p/menu/update'),
                    $concat('menu', '菜单删除', 'p/menu/del'),
                    $concat('rule', '权限规则列表', 'p/rule/index'),
                    $concat('rule', '权限规则创建', 'p/rule/create'),
                    $concat('rule', '资源规则', 'p/rule/save'),
                    $concat('rule', '权限规则查看', 'p/rule/re'),
                    $concat('rule', '权限规则编辑', 'p/rule/edit'),
                    $concat('rule', '更新权限规则', 'p/rule/update'),
                    $concat('rule', '权限规则删除', 'p/rule/del'),
                    $concat('user_rule_apply', '权限申请列表', 'p/userRuleApply/index'),
                    $concat('user_rule_apply', '权限申请审核', 'p/userRuleApply/edit'),
                    $concat('user_rule_apply', '更新权限申请', 'p/userRuleApply/update'),
                    $concat('user_login', '登录日志列表', 'p/userLogin/index'),
                    $concat('database', '数据库备份', 's/database/setting'),
                    $concat('database', '备份日志', 's/database/log'),
                    $concat('database', '备份下载', 's/database/downlo'),
                    $concat('database', '执行备份', 's/database/backup'),
                    $concat('database', '备份发送到邮箱', 's/database/se'),
                    $concat('general', '常规配置页面展示', 's/general/setting'),
                    $concat('general', '常规配置保存', 's/general/saveSetting'),
                    $concat('general', '发送测试邮件', 's/general/emailTest'),
                    $concat('permission', '菜单管理', 's/permission/menu'),
                    $concat('permission', '保存菜单排序', 's/permission/setMenuSort'),
                    $concat('permission', '设置菜单状态', 's/permission/setMenuStatus'),
                    $concat('permission', '保存用户角色', 's/permission/saveUserRoles'),
                    $concat('permission', '保存用户菜单', 's/permission/saveUserMenus'),
                    $concat('permission', '保存用户权限', 's/permission/saveUserRules'),
                    $concat('permission', '保存角色菜单', 's/permission/saveRoleMenus'),
                    $concat('permission', '保存角色权限', 's/permission/saveRoleRules'),
                    $concat('wechat', '公众号设置', 's/wechat/setting'),
                    $concat('wechat', '素材库', 's/wechat/materialList'),
                    $concat('wechat', '微信公众号设置', 's/wechat/saveConfig'),
                    $concat('wechat', '素材预览', 's/wechat/materialPreview'),
                    $concat('wechat', '素材删除', 's/wechat/materialRemove'),
                    $concat('wechat', '重置微信公众号菜单', 's/wechat/resetMenu'),
                    $concat('wechat', '上传素材', 's/wechat/uploadMaterial'),
                ]
            );
        }
    }

    protected function role()
    {
        $table_name = 'seecms_user_role';

        /**
         * @return \Illuminate\Database\Query\Builder
         */
        $table = function () use ($table_name) {
            return DB::table($table_name);
        };

        $concat = function ($name, $description) {
            return [
                'name' => $name,
                'rule' => '',
                'menu' => '',
                'description' => $description,
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s'),
            ];
        };

        $data = $table()->where('id', 1)->first();
        if (empty($data)) {
            $table()->insert($concat('超级管理员', '拥有最高权限'));
        }

        $data = $table()->where('name', '权限管理员')->first();
        if (empty($data)) {
            $table()->insert($concat('权限管理员', '此组的用户申请权限可以直接通过而不用审核'));
        }

        $data = $table()->where('name', '管理员')->first();
        if (empty($data)) {
            $table()->insert($concat('管理员', '拥有基本的权限，用于分配菜单，审核权限申请等'));
        }
    }

    protected function user()
    {
        $table_name = 'seecms_user';

        $data = DB::table($table_name)->where('id', 1)->first();
        if (empty($data)) {
            DB::table($table_name)->insert(
                [
                    'username' => 'root',
                    'password' => '6aca4b10feaeb639346dccc5d9533fab18b532fd',
                    'nickname' => 'seecms',
                    'avatar' => '',
                    'phone' => '18888888888',
                    'email' => 'root@seecms.com',
                    'tries' => 0,
                    'encrypt' => 'root',
                    'status' => 1,
                    'created_at' => date('Y-m-d H:i:s'),
                    'updated_at' => date('Y-m-d H:i:s'),
                ]
            );
        }

        $data = DB::table($table_name)->where('username', 'permission')->first();
        if (empty($data)) {
            DB::table($table_name)->insert(
                [
                    'username' => 'permission',
                    // 123456
                    'password' => '6ff23d619aa8396dbe7ca9967c2c89c79a94fb24',
                    'nickname' => '权限管理员',
                    'avatar' => '',
                    'phone' => '18888888887',
                    'email' => 'permission@seecms.com',
                    'tries' => 0,
                    'encrypt' => 'permission',
                    'status' => 1,
                    'created_at' => date('Y-m-d H:i:s'),
                    'updated_at' => date('Y-m-d H:i:s'),
                ]
            );
        }

        $data = DB::table($table_name)->where('username', 'admin')->first();
        if (empty($data)) {
            DB::table($table_name)->insert(
                [
                    'username' => 'admin',
                    // 123456
                    'password' => 'cd5ea73cd58f827fa78eef7197b8ee606c99b2e6',
                    'nickname' => '管理员',
                    'avatar' => '',
                    'phone' => '18888888886',
                    'email' => 'admin@seecms.com',
                    'tries' => 0,
                    'encrypt' => 'admin',
                    'status' => 1,
                    'created_at' => date('Y-m-d H:i:s'),
                    'updated_at' => date('Y-m-d H:i:s'),
                ]
            );
        }
    }
}
